package com.zx.sercurity;

import android.content.Context;


import com.zx.sercurity.util.BatteryUtil;
import com.zx.sercurity.util.ChargingStateListener;

import java.lang.ref.WeakReference;

/**
 * 电量风控
 * 电量50分钟不变将会触发风控
 */
public class BatteryRiskCountDown extends CountDownAsyncRiskControl<Boolean> implements ChargingStateListener, Runnable {

    private BatteryUtil batteryUtil;
    private float batteryPercent = -1f;
    private WeakReference<Context> contextWeakReference;

    private AsyncResultWrapper<Boolean> resultWrapper =
            new AsyncResultWrapper<>(ProcessState.IDLE,false, riskType(), false);

    public BatteryRiskCountDown(AsyncRiskListener riskLiveData) {
        super(riskLiveData);
    }

    @Override
    public RiskEnum riskType() {
        return RiskEnum.BatteryRisk;
    }

    @Override
    protected long riskHolderTime() {
        return 60 * 60 * 1000;
    }

    @Override
    protected boolean startUp(Context context) {
        if (contextWeakReference == null){
            contextWeakReference = new WeakReference<>(context);
        }
        if (batteryUtil == null){
            batteryUtil = new BatteryUtil(context);
            batteryUtil.register(this);
        }

        batteryPercent = BatteryUtil.getBatteryPct(context);
        if (batteryPercent >= 0) {
            if (listener != null){
                resultWrapper =  resultWrapper.newState(ProcessState.RUNNING);
                listener.onAsyncRiskStateChange(resultWrapper);
            }
            return true;
        }else {
            return false;
        }
    }

    @Override
    protected void startUpCanceled() {
        if (listener != null){
            resultWrapper = resultWrapper.newState(ProcessState.STOP);
            listener.onAsyncRiskStateChange(resultWrapper);
        }
    }

    @Override
    protected Runnable onCountDownTrigger() {
        return this;
    }

    @Override
    public void onStateChanged() {
        Context context = contextWeakReference != null ? contextWeakReference.get() : null;
        if (context != null) {
            float batteryPct = BatteryUtil.getBatteryPct(context);
            if (batteryPct >= 0 && Math.abs(batteryPct - batteryPercent) >= 1f) {
                batteryPercent = batteryPct;
                resetCountdown();
            }
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if (batteryUtil != null){
            batteryUtil.unregister();
            batteryUtil = null;
        }
        if (contextWeakReference != null) {
            contextWeakReference.clear();
            contextWeakReference = null;
        }
        if (listener != null){
            resultWrapper = resultWrapper.newState(ProcessState.DESTROY);
            listener.onAsyncRiskStateChange(resultWrapper);
        }
    }

    @Override
    public void run() {
        if (listener != null){
            resultWrapper = resultWrapper.newResult(true);
            resultWrapper = resultWrapper.inRisk(true);
            listener.onFindAsyncRisk(resultWrapper);
        }
    }
}

